package com.haohan.jingyuan.customer.controller;

import cn.hutool.json.JSONUtil;
import com.haohan.jingyuan.customer.dto.req.VisitorLoginRequest;
import com.haohan.jingyuan.customer.dto.resp.VisitorLoginResp;
import com.haohan.jingyuan.customer.service.CustomerVisitorService;
import com.haohan.jingyuan.gateway.dto.GwRequest;
import com.haohan.jingyuan.gateway.dto.GwResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * 客户访客控制器
 *
 * @ClassName CustomerVisitorController
 * @Description 客户访客相关接口控制器
 * @Author hexijun
 * @Time 2025/11/05 15:00
 * @Version 1.0
 */
@Slf4j
@RestController
@RequiredArgsConstructor
public class CustomerVisitorController {

    /**
     * 客户访客服务
     */
    private final CustomerVisitorService customerVisitorService;

    /**
     * 访客登录
     *
     * @param request 访客登录请求
     * @return 访客登录响应，包含按客户ID分组、文件按分组层级组织的数据
     */
    @PostMapping("/customer/visitor/login")
    public GwResponse<VisitorLoginResp> visitorLogin(@Valid @RequestBody GwRequest<VisitorLoginRequest> request) {
        log.info("CustomerVisitorController.visitorLogin BEGIN ==> {}", JSONUtil.toJsonStr(request));

        VisitorLoginResp loginResp = customerVisitorService.visitorLogin(request.getRequestData());
        GwResponse<VisitorLoginResp> response = null;
        if (loginResp == null) {
            response = GwResponse.error("用户名或密码错误");
        } else {
            response = GwResponse.success("登录成功", loginResp);
        }

        log.info("CustomerVisitorController.visitorLogin END ==> {}", JSONUtil.toJsonStr(response));
        return response;
    }
}

